package com.example.library.controller;

import com.example.library.model.BorrowRecord;
import com.example.library.service.BorrowService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/borrows")
public class BorrowController {
    private final BorrowService borrowService;

    public BorrowController(BorrowService borrowService) {
        this.borrowService = borrowService;
    }

    @GetMapping("/user/{userId}")
    public ResponseEntity<Page<BorrowRecord>> getUserBorrowHistory(
            @PathVariable Long userId,
            Pageable pageable) {
        return ResponseEntity.ok(borrowService.getUserBorrowHistory(userId, pageable));
    }

    @PostMapping
    public ResponseEntity<BorrowRecord> borrowBook(
            @RequestParam Long userId,
            @RequestParam Long bookId) {
        return ResponseEntity.ok(borrowService.borrowBook(userId, bookId));
    }
}